



import bpy
from 全局控制.Node.节点和插座基类 import bl_容器节点 as 容器
import 全局控制.Node.节点和插座基类.bl_节点基类 as 基类
import 全局控制.Node.插座.bl_插座基类 as 插座基类
import 节点.Core.bl_UI配色 as 配色

import engine.initLIB.load_dll as engineLib
import ctypes


g_迭代器元素 = {}



engineLib.flexCAPI.f_创建循环节点.restype = ctypes.c_bool
engineLib.flexCAPI.f_创建函数节点.restype = ctypes.c_bool







class C_Node_组输入(基类.C_Node_基类): 
	bl_label = '容器'
	bl_dname = 'bl_node_组输入'
	#bl_static_type = 'UNDEFINED'

	m_RootNode = bpy.props.StringProperty()
	def init(self, context):
		super(C_Node_组输入, self).f_init(context)
		self.name = 'Group Input'
		#self.outputs.new('C_NodeSocket_创建插座', name='__创建插座__')
		self.f_toCPP(self.id_data)

		self.use_custom_color = True
		self.color = 配色.g_内部节点颜色
		
	def f_toCPP(self, tree):
		pass
		#engineLib.flexCAPI.f_创建函数节点(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), self.f_getPyObject());
		

class C_Node_组输出(基类.C_Node_基类): 
	bl_label = '容器'
	bl_dname = 'bl_node_组输出'
	#bl_static_type = 'UNDEFINED'
	
	m_RootNode = bpy.props.StringProperty()
	def init(self, context):
		super(C_Node_组输出, self).f_init(context)
		self.name = 'Group Output'
		self.f_toCPP(self.id_data)

		self.use_custom_color = True
		self.color = 配色.g_内部节点颜色
		
	def f_toCPP(self, tree):
		pass
		#engineLib.flexCAPI.f_创建函数节点(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), self.f_getPyObject());




class C_Node_迭代器(基类.C_Node_基类): 
	bl_label = '迭代器'
	bl_dname = 'bl_node_迭代器'

	def init(self, context):
		super(C_Node_迭代器, self).f_init(context)
		self.name = '迭代器'
		self.inputs.new('C_Socket_Vector模板', name='表')
		self.outputs.new('C_Socket_引用', name='元素')
		self.outputs.new('C_Socket_I32', name='ID')

		self.f_toCPP(self.id_data)
		self.use_custom_color = True
		self.color = 配色.g_内部节点颜色

	def f_toCPP(self, tree):
		pass
	#	pyObj = bpy.data.node_groups[self.id_data.name].nodes[self.name]
	#	engineLib.flexCAPI.f_创建迭代器节点(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), ctypes.py_object(pyObj));
		
		





def f_包节点内部输入输出插座构建(self):
	index = 0
	for socket in self.inputs:
		if socket.bl_idname == 'C_Socket_节点流程阀' : 
			index += 1
			continue
		data = ctypes.py_object([list(socket.bl_idname), list(socket.identifier), int(socket.is_output), index])
		engineLib.flexCAPI.f_bl_创建节点插座(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), data)
		socket.f_update()
		index += 1
	index = 0
	for socket in self.outputs:
		print('socket.bl_idname = ', socket.bl_idname, index)
		if socket.bl_idname == 'C_Socket_节点流程阀' : 
			index += 1
			continue
		data = ctypes.py_object([list(socket.bl_idname), list(socket.identifier), int(socket.is_output), index])
		engineLib.flexCAPI.f_bl_创建节点插座(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), data)
		socket.f_update()
		index += 1






class C_Node_自定义容器(基类.C_Node_基类): 
	bl_label = '容器'
	bl_dname = 'bl_node_容器'
	bl_static_type = 'GROUP'
	
	#m_TreeName = bpy.props.StringProperty(name='sceneName', default='pack_scene')
	def init(self, context):
		super(C_Node_自定义容器, self).init(context)

	def f_init(self, context):
		super(C_Node_自定义容器, self).f_init(context)
		self.use_custom_color = True
		self.color = 配色.g_容器节点颜色
		#self.f_toCPP()

	def draw_buttons_ext(self, context, layout):
		#layout.prop(self, 'node_tree')
		super(C_Node_自定义容器, self).draw_buttons_ext(context, layout)

	def f_进入子集(self):
		pass
	def f_推出子集(self):
		pass
	def f_get树(self):
		return self.m_Tree
	






class C_Node_函数(C_Node_自定义容器): 
	bl_label = '函数'
	bl_dname = 'bl_node_函数'
	
	#m_TreeName = bpy.props.StringProperty(name='sceneName', default='pack_scene')
	m_Tree = bpy.props.PointerProperty(name='物体控制节点树', type=bpy.types.NodeTree)
	m_是否执行 = bpy.props.BoolProperty(default=False)
	def init(self, context):
		super(C_Node_函数, self).f_init(context)

		self.m_Tree = bpy.data.node_groups.new('pack_scene', 'idname_场景对象操作节点树')
		self.m_Tree.m_父节点树 = self.id_data

		i_node = self.m_Tree.nodes.new(type='C_Node_组输入')
		i_node.location.x = -400
		o_node = self.m_Tree.nodes.new(type='C_Node_组输出')
		o_node.location.x = 400
		
		self.f_toCPP(self.id_data)
		
		
		
	def f_toCPP(self, tree):
		是否更新 = engineLib.flexCAPI.f_创建函数节点(ctypes.c_wchar_p(tree.name), ctypes.c_wchar_p(self.name), self.f_getPyObject(tree));
		if 是否更新 :
			data = [ list("tree_name"), list(self.m_Tree.name), bpy.data.node_groups[self.m_Tree.name], self.m_Tree.nodes['Group Input'], self.m_Tree.nodes['Group Output'] ]
			engineLib.flexCAPI.f_node_传入BL数据(ctypes.c_wchar_p(tree.name), ctypes.c_wchar_p(self.name), ctypes.py_object(data));
			
			f_包节点内部输入输出插座构建(self)
			print('C_Node_函数 创建完毕')
		
	def f_进入子集(self):
		super(C_Node_函数, self).f_进入子集()
	def f_推出子集(self):
		super(C_Node_函数, self).f_推出子集()
		
	def f_release(self):
		self.m_Tree.use_fake_user = False
		bpy.context.space_data.node_tree = None
		
		self.m_Tree.user_clear()
		bpy.data.node_groups.remove(self.m_Tree)
		

class C_NODE_AddMenu_函数(bpy.types.Operator):
	bl_idname = 'node.global_node_fun'
	bl_label = '函数'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		node = bpy.ops.node.add_node(type='C_Node_函数', use_transform=True)
		return {'FINISHED'}











class C_Node_循环(C_Node_自定义容器): 
	bl_label = '循环'
	bl_dname = 'bl_node_循环'
	
	m_Tree = bpy.props.PointerProperty(name='物体控制节点树', type=bpy.types.NodeTree)
	m_是否执行 = bpy.props.BoolProperty(default=False)
	def init(self, context):
		super(C_Node_循环, self).f_init(context)

		self.m_Tree = bpy.data.node_groups.new('pack_scene', 'idname_场景对象操作节点树')
		self.m_Tree.m_父节点树 = self.id_data
		
		迭代器 = self.m_Tree.nodes.new(type='C_Node_迭代器')
		迭代器.location.x = -400
		i_node = self.m_Tree.nodes.new(type='C_Node_组输入')
		i_node.location.x = -500
		o_node = self.m_Tree.nodes.new(type='C_Node_组输出')
		o_node.location.x = 400
		
		self.f_toCPP(self.id_data)
		
		
	def f_toCPP(self, tree):
		是否更新 = engineLib.flexCAPI.f_创建循环节点(ctypes.c_wchar_p(tree.name), ctypes.c_wchar_p(self.name), self.f_getPyObject(tree));
		#super(C_Node_循环, self).f_toCPP()
		if 是否更新 :
			data = [ list("tree_name"), list(self.m_Tree.name), bpy.data.node_groups[self.m_Tree.name], self.m_Tree.nodes['Group Input'], self.m_Tree.nodes['Group Output'] ]
			engineLib.flexCAPI.f_node_传入BL数据(ctypes.c_wchar_p(tree.name), ctypes.c_wchar_p(self.name), ctypes.py_object(data));
			
			data = [list("tree_loop"), list("迭代器"), self.m_Tree.nodes['迭代器']]
			engineLib.flexCAPI.f_node_传入BL数据(ctypes.c_wchar_p(tree.name), ctypes.c_wchar_p(self.name), ctypes.py_object(data));

			f_包节点内部输入输出插座构建(self)
			

	def draw_label(self):
		return '循环'
	def f_进入子集(self):
		super(C_Node_循环, self).f_进入子集()
	def f_推出子集(self):
		super(C_Node_循环, self).f_推出子集()

	def f_release(self):
		#tree = context.space_data.node_tree
		self.m_Tree.use_fake_user = False
		bpy.context.space_data.node_tree = None
		
		self.m_Tree.user_clear()
		bpy.data.node_groups.remove(self.m_Tree)
		
		

class C_NODE_AddMenu_循环(bpy.types.Operator):
	bl_idname = 'node.global_node_loop'
	bl_label = '循环'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		node = bpy.ops.node.add_node(type='C_Node_循环', use_transform=True)
		return {'FINISHED'}









classes = (
	C_Node_组输入,
	C_Node_组输出,

	C_Node_自定义容器,
	C_Node_迭代器,


	C_Node_函数,
	C_NODE_AddMenu_函数,

	C_Node_循环,
	C_NODE_AddMenu_循环,

)

def register():
	for c in classes:
		bpy.utils.register_class(c)
		
def unregister():
	for c in classes:
		bpy.utils.unregister_class(c)

	

